<template>
    <div>
        <h3>门店增加</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">门店名称</td>
                    <td><input type="text" v-model="Query.MName" ></td>
                </tr>
                <tr>
                    <td align="right">分店名称</td>
                    <td><input type="text" v-model="Query.FName"></td>
                </tr>
                <tr>
                    <td align="right">分店地址</td>
                    <td><input type="text" v-model="Query.Dizhi"></td>
                </tr>
                <tr>
                    <td align="right">图片</td>
                    <td><input type="file"  @change="img">
                        <img :src="Query.Photo" alt="" width="100" height="100" v-if="Query.Photo!=''">
                    </td>
                </tr>
                <tr>
                    <td align="right">电话</td>
                    <td><input type="text" v-model="Query.KiPhone"></td>
                </tr>
                <tr>
                    <td align="right">状态</td>
                    <td><input type="radio" name="sex" :value="1" v-model="Query.State">启用
                        <input type="radio" name="sex" :value="0" v-model="Query.State">禁用
                    </td>
                </tr>
                <tr>
                    <td align="right">门店介绍</td>
                    <td>
                        <textarea cols="50" rows="5" v-model="Query.MenDianJs"></textarea>
                    </td>
                </tr>
                <tr>
                    <td align="right">入住须知</td>
                    <td><textarea cols="50" rows="5" v-model="Query.Ruzhu"></textarea></td>
                </tr>
                <tr>
                    <td align="right">交通信息</td>
                    <td><textarea cols="50" rows="5" v-model="Query.JiaoTong"></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import axios from 'axios';
import moment from 'moment';
import { useRoute, useRouter } from 'vue-router';
const Route = useRoute();
const Router = useRouter();

let Query:any=reactive({
    "MName": "",
    "FName": "",
    "Dizhi": "",
    "Photo": "",
    "KiPhone": "",
    "State": 1,
    "MenDianJs": "",
    "Ruzhu": "",
    "JiaoTong": "",
})
const img=(e:any)=>{
    let f=e.target.files[0];
    let fs=new FormData();
    fs.append('file',f)
    axios({
        url:'/api/Img/Img',
        method:'post',
        data:fs
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="图片格式不正确")
        {
            alert("图片格式不正确");
            return;
        }
        if(res.data=="图片大小不得大于2M")
        {
            alert("图片大小不得大于2M");
            return;
        }
        Query.Photo=res.data;
    })
    .catch((err)=>{
        console.log(err);
        
    })
}
const add=()=>{
    if(Query.MName=="")
    {
        alert("门店名称不能为空");
        return;
    }
    if(Query.FName=="")
    {
        alert("分店名称不能为空");
        return;
    }
    if(Query.Dizhi=="")
    {
        alert("地址不能为空");
        return;
    }
    if(Query.Photo=="")
    {
        alert("图片不能为空");
        return;
    }
    if(Query.KiPhone=="")
    {
        alert("手机号不能为空");
        return;
    }
    if(Query.State=="")
    {
        alert("状态不能为空");
        return;
    }
    if(Query.MenDianJs=="")
    {
        alert("介绍不能为空");
        return;
    }
    if(Query.Ruzhu=="")
    {
        alert("入住须知不能为空");
        return;
    }
    if(Query.JiaoTong=="")
    {
        alert("交通信息不能为空");
        return;
    }
    axios({
        url:'/api/House/AddHouse',
        method:'post',
        data:Query
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1)
        {
            alert("门店已存在");
            return;
        }
        else if(res.data>0)
        {
            alert("添加成功");
            return;
        }
        else{
            alert("添加失败");
            return;
        }
    })
    .catch((err)=>{
        console.log(err);
    })
}
</script>

<style scoped>

</style>